package controller.v2;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import model.AddressVO;
import model.CatVO;
import model.GoodVO;
import model.MemberMsgVO;
import model.MemberVO;
import model.ShowwindowCatVO;
import model.ShowwindowCommentVO;
import model.ShowwindowPraiseVO;
import model.ShowwindowVO;
import service.v2.CatService;
import service.v2.ConfigService;
import service.v2.GoodService;
import service.v2.MemberService;
import service.v2.MessageService;
import service.v2.PushService;
import service.v2.ShowwindowService;
import util.PackageUtil;
import util.PropertieUtil;
import util.PushUtil;
import util.StringUtil;

@Controller
@RequestMapping("/v2/window")
public class ShowwindowController {
	@Autowired
	private ShowwindowService showwindowService;
	@Autowired
	private CatService catService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private GoodService goodService;
	@Autowired
	private ConfigService configService;
	@Autowired
	private MessageService messageService;
	@Autowired
	private PushService pushservice;
	/**
	 * 我的橱窗
	 * member_id
	 * */
	@ResponseBody
	@RequestMapping(value = "/myWindow" , method = RequestMethod.GET)
	public Object myWindow(@RequestHeader(value = "OS", required = false) String os, @RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String header_member_id, String member_id, MemberVO memberVO) throws Exception {
		if(member_id == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(member_id.equals(header_member_id)){
			MemberVO vo = memberService.getMemberByIdCheck(member_id, token, os);
			if (vo == null) {
				return PackageUtil.packageParams(403, "forbidden", "", "");
			}
		}
		//橱窗人的信息
		memberVO = memberService.getSimpleInfoById(member_id);
		//收藏
		int window_num = showwindowService.getWindowNumByMember(member_id);
		//获赞
		int praise_num = showwindowService.getPraiseNumByMember(member_id);
		//检查是否有默认橱窗
		ShowwindowCatVO windowCatVO =  showwindowService.getDefaultWindowCat(member_id);
		if(windowCatVO == null){
			showwindowService.insertDefaultWindowCat(member_id);
		}
		//橱窗列表
		List<ShowwindowCatVO> windowCatList = showwindowService.getWidowCatListByMember(member_id);
		//图片缩放
		if(screenwidth != null && StringUtil.isNumeric(screenwidth)){
			showwindowService.processCatImg(windowCatList, screenwidth,2);
		}
		Map map = new HashMap();
		map.put("member", memberVO);
		map.put("window_num", window_num);
		map.put("praise_num", praise_num);
		map.put("windowCatList", windowCatList);
		
		Map share = new HashMap();
		//分享链接IMG
		share.put("image",memberVO.getLogo()+"?x-oss-process=image/resize,h_100");
		//分享链接URLz
		share.put("url",configService.loadConfig("WECHAT_HOST")+"/window_windowList?member_id="+memberVO.getMember_id()+"&ym_share_inviteno="+memberService.getInvitenoById(header_member_id));
		//分享链接title
		share.put("title", memberVO.getNick_name());
		//分享链接content
		share.put("content",memberVO.getPersonal_sign());
		map.put("share", share);
		return PackageUtil.packageParams(200, "successs", map, "");
	}
	
	/**
	 * 根据分类查看橱窗列表1
	 * cat_id 橱窗分类id
	 * 
	 * */
	@ResponseBody
	@RequestMapping(value = "/catWindowList" , method = RequestMethod.GET)
	public Object catWindowList(@RequestHeader(value = "OS", required = false) String os, @RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String header_member_id, MemberVO memberVO, ShowwindowVO showwindowVO) throws Exception {
		if(showwindowVO.getCat_id() == null || showwindowVO.getMember_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(showwindowVO.getMember_id().equals(header_member_id)){
			MemberVO vo = memberService.getMemberByIdCheck(showwindowVO.getMember_id(), token,os);
			if (vo == null) {
				return PackageUtil.packageParams(403, "forbidden", "", "");
			}
		}

		//橱窗人的信息
		memberVO = memberService.getSimpleInfoById(showwindowVO.getMember_id());
		//橱窗分类信息
		ShowwindowCatVO showwindowCatVO = showwindowService.getWidowCatById(showwindowVO.getCat_id());
		//商品总数
		int good_num = showwindowService.getGoodnumByWindowCat(showwindowVO.getCat_id());
		//商品分类
		List<CatVO> goodCatList = showwindowService.getGoodCatListByWindowCat(showwindowVO.getCat_id());
		Map map = new HashMap();
		map.put("member", memberVO);
		map.put("showwindowCatVO", showwindowCatVO);
		map.put("good_num", good_num);
		map.put("goodCatList", goodCatList);
		
		Map share = new HashMap();
		//分享链接IMG
		share.put("image",memberVO.getLogo()+"?x-oss-process=image/resize,h_100");
		//分享链接URL
		share.put("url", configService.loadConfig("WECHAT_HOST")+"/window_windowGood?member_id="+memberVO.getMember_id()+"&id="+showwindowVO.getCat_id()+"&ym_share_inviteno="+memberService.getInvitenoById(header_member_id));
		//分享链接title
		share.put("title", showwindowCatVO.getName());
		//分享链接content
		share.put("content",showwindowCatVO.getIntro());
		map.put("share", share);
		
		return PackageUtil.packageParams(200, "successs", map, "");
	}
	/**
	 * 根据分类查看橱窗列表2
	 * cat_id 橱窗分类id
	 * good_cat 商品分类id
	 * */
	@ResponseBody
	@RequestMapping(value = "/catWindowListContent" , method = RequestMethod.GET)
	public Object catWindowListContent(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowVO showwindowVO) throws Exception {
		if(showwindowVO.getCat_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		showwindowVO.setMember_id(member_id);
		int total = showwindowService.getShowwindowListByWindowCatTotal(showwindowVO);
		showwindowVO.setPagination(showwindowVO.getPage_index(), total+"");
		//橱窗列表
		List<ShowwindowVO> showwindowList = showwindowService.getShowwindowListByWindowCat(showwindowVO);
		//图片缩放
		if(screenwidth != null && StringUtil.isNumeric(screenwidth)){
			showwindowService.processImg(showwindowList, screenwidth,2);
		}
		Map map = new HashMap();
		map.put("showwindowList", showwindowList);
		map.put("pagination", showwindowVO);
		return PackageUtil.packageParams(200, "successs", map, "");
	}
	/**
	 * 橱窗详情
	 * window_id 橱窗id
	 * */
	@ResponseBody
	@RequestMapping(value = "/windowDetail" , method = RequestMethod.GET)
	public Object windowDetail(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowVO showwindowVO) throws Exception {
		if(showwindowVO.getWindow_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		//橱窗信息
		showwindowVO = showwindowService.getWindowById(showwindowVO.getWindow_id());
		if(showwindowVO == null){
			return PackageUtil.packageParams(15001,"抱歉，您查找的内容已被删除","",""); 
		}

		//橱窗人的信息
		memberVO = memberService.getSimpleInfoById(showwindowVO.getMember_id());
		//商品信息
		GoodVO goodVO = goodService.getGoodInfoById(showwindowVO.getGood_id(), PropertieUtil.loadPropertie("good_imgs")); 
		//橱窗分类的数量
		int window_cat_num = showwindowService.getWindowCatNumByMember(showwindowVO.getMember_id());
		//评论总数
		int comment_num = showwindowService.getCommentNumByShowwindow(showwindowVO.getWindow_id());
		//评论
		List<ShowwindowCommentVO> commentList = showwindowService.getCommentListByWindow(showwindowVO.getWindow_id());
		Map map = new HashMap();
		//登录
		if(member_id != null){
			//点赞状态
			ShowwindowPraiseVO praiseVO = showwindowService.checkPraiseByShowwindow(showwindowVO.getWindow_id(), member_id);
			map.put("praise_state", praiseVO==null || "Y".equals(praiseVO.getDelete_yn()) ? 0 : 1);
		}
		map.put("showwindow", showwindowVO);
		map.put("member", memberVO);
		map.put("good", goodVO);
		map.put("window_cat_num", window_cat_num);
		map.put("comment_num", comment_num);
		map.put("commentList", commentList);
		
		Map share = new HashMap();
		//分享链接IMG
		share.put("image",goodVO.getImg()+"?x-oss-process=image/resize,h_100");
		//分享链接URL
		share.put("url", configService.loadConfig("WECHAT_HOST")+"/window_windowGoodDetail?window_id="+showwindowVO.getWindow_id()+"&ym_share_inviteno="+memberService.getInvitenoById(member_id));
		//分享链接title
		share.put("title",  goodVO.getName());
		//分享链接content
		share.put("content",showwindowVO.getIntro());
		map.put("share", share);
		return PackageUtil.packageParams(200, "successs", map, "");
	}
	/**
	 * 点赞
	 * showwindow_id
	 */
	@ResponseBody
	@RequestMapping(value = "/praiseInsertSub" , method = RequestMethod.POST)
	public Object praiseInsertSub(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response,
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO,
			ShowwindowVO showwindowVO, ShowwindowPraiseVO praiseVO) throws Exception {
		if(praiseVO.getShowwindow_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		//橱窗信息
		showwindowVO = showwindowService.getWindowById(praiseVO.getShowwindow_id());
		if(showwindowVO == null){
			return PackageUtil.packageParams(15001,"抱歉，您查找的内容已被删除","",""); 
		}
		ShowwindowPraiseVO showwindowPraiseVO = showwindowService.checkPraiseByShowwindow(praiseVO.getShowwindow_id(), member_id);
		if(showwindowPraiseVO == null || "Y".equals(showwindowPraiseVO.getDelete_yn())){
			if(showwindowPraiseVO == null){
				showwindowService.insertPraise(praiseVO.getShowwindow_id(), member_id);
			}else if( "Y".equals(showwindowPraiseVO.getDelete_yn())){
				showwindowService.updatePraiseVODelete(praiseVO.getShowwindow_id(), showwindowPraiseVO.getId());
			}
			if(!member_id.equals(showwindowVO.getMember_id())){
				//推送
				memberVO = memberService.getAllInfoById(showwindowVO.getMember_id());
				/*Map map = new HashMap();
				map.put("title", configService.loadConfig("APP_NAME"));
				map.put("content", "您收到了新的赞");
				map.put("type", 4);
				//PushUtil.getuiPushSingle(memberVO.getPush_id(), configService.loadConfig("APP_LOGO"),  configService.loadConfig("APP_NAME"), "您收到了新的赞", "", JSON.toJSONString(map));*/
				pushservice.praiseInsertSub(memberVO.getPush_id());
			}
		}
		return PackageUtil.packageParams(200, "successs", "", "");
	}
	/**
	 * 取消点赞
	 * showwindow_id
	 */
	@ResponseBody
	@RequestMapping(value = "/praiseDelete" , method = RequestMethod.POST)
	public Object praiseDelete(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response,
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO,
			ShowwindowVO showwindowVO, ShowwindowPraiseVO praiseVO) throws Exception {
		if(praiseVO.getShowwindow_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		//橱窗信息
		showwindowVO = showwindowService.getWindowById(praiseVO.getShowwindow_id());
		if(showwindowVO == null){
			return PackageUtil.packageParams(15001,"抱歉，您查找的内容已被删除","",""); 
		}
		ShowwindowPraiseVO showwindowPraiseVO = showwindowService.checkPraiseByShowwindow(praiseVO.getShowwindow_id(), member_id);
		if(showwindowPraiseVO != null){
			showwindowService.removePraise(praiseVO.getShowwindow_id(), showwindowPraiseVO.getId());
		}
		return PackageUtil.packageParams(200, "successs", "", "");
	}
	/**
	 * 猜你喜欢
	 * @param good_id 商品ID
	 * @param page_index
	 * */
	@ResponseBody
	@RequestMapping(value = "/windowRecommandList" , method = RequestMethod.GET)
	public Object windowRecommandList(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response,
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO,
			ShowwindowVO showwindowVO) throws Exception {
		if(showwindowVO.getGood_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		showwindowVO.setLogin_member_id(member_id);
		int total = showwindowService.getRecommandTotal(showwindowVO);
		showwindowVO.setPagination(showwindowVO.getPage_index(), total + "");
		List<ShowwindowVO> showwindowList = showwindowService.getRecommandList(showwindowVO);
		Map map = new HashMap();
		map.put("recommandWindowList", showwindowList);
		map.put("pagination", showwindowVO);
		return PackageUtil.packageParams(200, "successs", map, "");
	}
	
	/**
	 * 添加橱窗分类
	 * name 名称
	 * intro 描述
	 */
	@ResponseBody
	@RequestMapping(value = "/windowCatInsert" , method = RequestMethod.POST)
	public Object windowCatInsert(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowCatVO showwindowCatVO) throws Exception {
		if(showwindowCatVO.getName() == null || showwindowCatVO.getIntro() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(showwindowCatVO.getName().length() < 2 || showwindowCatVO.getName().length() > 40 || showwindowCatVO.getIntro().length() > 200){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		int total = showwindowService.getWindowCatNumByMember(member_id);
		if(total >= 20){
			return PackageUtil.packageParams(15005,"橱窗不能超过20个","",""); 
		}
		showwindowCatVO.setMember_id(member_id);
		showwindowService.insertWindowCat(showwindowCatVO);
		return PackageUtil.packageParams(200, "successs", showwindowCatVO, "");
	}
	
	/**
	 * 修改橱窗分类
	 * name 名称
	 * intro 描述
	 * showwindow_cat_id 橱窗分类ID
	 */
	@ResponseBody
	@RequestMapping(value = "/windowCatUpdate" , method = RequestMethod.POST)
	public Object windowCatUpdate(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowCatVO showwindowCatVO) throws Exception {
		if(showwindowCatVO.getName() == null || showwindowCatVO.getIntro() == null || showwindowCatVO.getShowwindow_cat_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(showwindowCatVO.getName().length() < 2 || showwindowCatVO.getName().length() > 40 || showwindowCatVO.getIntro().length() > 200){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		showwindowCatVO.setMember_id(member_id);
		showwindowService.updateWindowCat(showwindowCatVO);
		return PackageUtil.packageParams(200, "successs", "", "");
	}
	
	/**
	 * 删除橱窗分类
	 * showwindow_cat_id 橱窗分类ID
	 */
	@ResponseBody
	@RequestMapping(value = "/windowCatDelete" , method = RequestMethod.POST)
	public Object windowCatDelete(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowCatVO showwindowCatVO) throws Exception {
		if(showwindowCatVO.getShowwindow_cat_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		showwindowCatVO.setMember_id(member_id);
		showwindowService.deleteWindowCat(showwindowCatVO);
		return PackageUtil.packageParams(200, "successs", "", "");
	}
	
	/**
	 * 橱窗分类列表
	 */
	@ResponseBody
	@RequestMapping(value = "/windowCatList" , method = RequestMethod.GET)
	public Object windowCatList(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowCatVO showwindowCatVO) throws Exception {
		//检查是否有默认橱窗
		ShowwindowCatVO windowCatVO =  showwindowService.getDefaultWindowCat(member_id);
		if(windowCatVO == null){
			showwindowService.insertDefaultWindowCat(member_id);
		}
		showwindowCatVO.setMember_id(member_id);
		List<ShowwindowCatVO> catList = showwindowService.getWindowCatList(showwindowCatVO);
		return PackageUtil.packageParams(200, "successs", catList , "");
	}
	
	/**橱窗修改
	 * @param intro
	 * @param cat_id
	 * @param window_id
	 */
	@ResponseBody
	@RequestMapping(value = "/windowUpdateSub" , method = RequestMethod.POST)
	public Object windowUpdateSub(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowVO showwindowVO) throws Exception {
		if(showwindowVO.getWindow_id() == null || showwindowVO.getCat_id() == null || showwindowVO.getIntro() ==null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(showwindowVO.getIntro().length() > 200){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		ShowwindowCatVO catVO = showwindowService.getWidowCatById(showwindowVO.getCat_id());
		if(catVO == null){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		showwindowVO.setMember_id(member_id);
		showwindowService.updateShowwindow(showwindowVO);
		return PackageUtil.packageParams(200, "successs", "" , "");
	}
	
	/**橱窗删除
	 * @param window_id
	 */
	@ResponseBody
	@RequestMapping(value = "/windowDelete" , method = RequestMethod.POST)
	public Object windowDelete(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowVO showwindowVO) throws Exception {
		if(showwindowVO.getWindow_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		showwindowVO.setMember_id(member_id);
		showwindowService.deleteShowwindow(showwindowVO);
		return PackageUtil.packageParams(200, "successs", "" , "");
	}
	
	/**
	 * 评论列表
	 * window_id 橱窗id
	 * page_index 分页
	 * */
	@ResponseBody
	@RequestMapping(value = "/windowCommentList" , method = RequestMethod.GET)
	public Object windowCommentList(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowVO showwindowVO) throws Exception {
		if(showwindowVO.getWindow_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		//橱窗信息
		ShowwindowVO vo = showwindowService.getWindowById(showwindowVO.getWindow_id());
		if(vo == null){
			return PackageUtil.packageParams(15001,"抱歉，您查找的内容已被删除","",""); 
		}
		//评论
		int total = showwindowService.getCommentNumByShowwindow(showwindowVO.getWindow_id());
		showwindowVO.setPagination(showwindowVO.getPage_index(), total+"");
		List<ShowwindowCommentVO> commentList = showwindowService.getCommentList(showwindowVO);
		
		Map map = new HashMap();
		map.put("showwindow", vo);
		map.put("pagination", showwindowVO);
		map.put("commentList", commentList);
		return PackageUtil.packageParams(200, "successs", map, "");
	}
	
	/**
	 * 验证商品添加
	 * good_id
	 * cat_id
	 */
	@ResponseBody
	@RequestMapping(value = "/windowInsertCheck" , method = RequestMethod.GET)
	public Object windowInsertCheck(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowVO showwindowVO) throws Exception {
		showwindowVO.setMember_id(member_id);
		if(showwindowVO.getGood_id() == null || showwindowVO.getCat_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		int good_num = showwindowService.checkGoodState(showwindowVO.getGood_id());
		if(good_num == 0){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		int cat_num = showwindowService.checkWindowCat(showwindowVO);
		if(cat_num == 0){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		ShowwindowVO vo = showwindowService.checkWindowGood(showwindowVO);
		if(vo != null){
			return PackageUtil.packageParams(15002,"已添加至此橱窗，不可重复添加",vo,""); 
		}
		return PackageUtil.packageParams(200, "successs", "" , "");
	}
	
	
	/**
	 * 商品收藏
	 * good_id
	 * intro
	 * cat_id
	 */
	@ResponseBody
	@RequestMapping(value = "/windowInsertSub" , method = RequestMethod.POST)
	public Object windowInsertSub(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowVO showwindowVO) throws Exception {
		showwindowVO.setMember_id(member_id);
		if(showwindowVO.getGood_id() == null || showwindowVO.getIntro() == null || showwindowVO.getCat_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(showwindowVO.getIntro().length() > 200){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		int good_num = showwindowService.checkGoodState(showwindowVO.getGood_id());
		if(good_num == 0){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		int cat_num = showwindowService.checkWindowCat(showwindowVO);
		if(cat_num == 0){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		ShowwindowVO vo = showwindowService.checkWindowGood(showwindowVO);
		if(vo != null){
			return PackageUtil.packageParams(15002,"已添加至此橱窗，不可重复添加",vo,""); 
		}
		showwindowService.insertShowwindow(showwindowVO);
		return PackageUtil.packageParams(200, "successs", "" , "");
	}
	 /**
	  * 评论
	  * showwindow_id 橱窗ID
	  * content 内容
	  */
	@ResponseBody
	@RequestMapping(value = "/windowCommentInsertSub" , method = RequestMethod.POST)
	public Object windowCommentInsertSub(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowCommentVO commentVO) throws Exception {
		if(commentVO.getShowwindow_id() == null || commentVO.getContent() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(commentVO.getContent().length() > 200){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		//橱窗信息
		ShowwindowVO showwindowVO = showwindowService.getWindowById(commentVO.getShowwindow_id());
		if(showwindowVO == null){
			return PackageUtil.packageParams(15001,"抱歉，您查找的内容已被删除","",""); 
		}
		commentVO.setCreate_by(member_id);
		showwindowService.insertComment(commentVO);
		//推送
		if(!showwindowVO.getMember_id().equals(member_id)){
			memberVO = memberService.getAllInfoById(showwindowVO.getMember_id());
			/*Map map = new HashMap();
			map.put("title", configService.loadConfig("APP_NAME"));
			map.put("content", "您收到了新的评论");
			map.put("type", 4);
			//PushUtil.getuiPushSingle(memberVO.getPush_id(), configService.loadConfig("APP_LOGO"),  configService.loadConfig("APP_NAME"), "您收到了新的评论", "", JSON.toJSONString(map));*/
			pushservice.pushwindowCommentInsertSub(memberVO.getPush_id());
		}
		return PackageUtil.packageParams(200, "successs", "" , "");
	}
	
	 /**
	  * 评论回复
	  * comment_id 评论ID
	  * content 内容
	  */
	@ResponseBody
	@RequestMapping(value = "/windowCommentReply" , method = RequestMethod.POST)
	public Object windowCommentReply(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowCommentVO commentVO) throws Exception {
		if(commentVO.getComment_id() == null || commentVO.getContent() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		ShowwindowCommentVO vo = showwindowService.getCommentById(commentVO.getComment_id());
		if(vo == null){
			return PackageUtil.packageParams(15003,"无此评论","",""); 
		}
		/*if("Y".equals(vo.getDelete_yn())){
			return PackageUtil.packageParams(15004,"该评论已删除","",""); 
		}*/
		if(!"1".equals(vo.getLevel()) || commentVO.getContent().length() > 200){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		commentVO.setCreate_by(member_id);
		showwindowService.insertCommentReply(commentVO);
		if(!vo.getCreate_by().equals(member_id)){
			//推送
			memberVO = memberService.getAllInfoById(vo.getCreate_by());
			/*Map map = new HashMap();
			map.put("title", configService.loadConfig("APP_NAME"));
			map.put("content", "您收到了新的评论");
			map.put("type", 4);
			//PushUtil.getuiPushSingle(memberVO.getPush_id(), configService.loadConfig("APP_LOGO"),  configService.loadConfig("APP_NAME"), "您收到了新的评论", "", JSON.toJSONString(map));*/
			pushservice.pushWindowCommentReply(memberVO.getPush_id());
		}
		return PackageUtil.packageParams(200, "successs", "" , "");
	}
	
	/**
	 * 回复回复
	 * comment_id 评论ID
	 * content 内容
	 */
	@ResponseBody
	@RequestMapping(value = "/windowCommentReplyBack" , method = RequestMethod.POST)
	public Object windowCommentReplyBack(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowCommentVO commentVO) throws Exception {
		if(commentVO.getComment_id() == null || commentVO.getContent() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		ShowwindowCommentVO vo = showwindowService.getCommentById(commentVO.getComment_id());
		if(vo == null){
			return PackageUtil.packageParams(15003,"无此评论","",""); 
		}
		if(!"2".equals(vo.getLevel()) || member_id.equals(vo.getCreate_by()) || commentVO.getContent().length() > 200){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		commentVO.setCreate_by(member_id);
		showwindowService.insertCommentReplyBack(commentVO);
		//推送
		memberVO = memberService.getAllInfoById(vo.getCreate_by());
		/*Map map = new HashMap();
		map.put("title", configService.loadConfig("APP_NAME"));
		map.put("content", "您收到了新的评论");
		map.put("type", 4);
		//PushUtil.getuiPushSingle(memberVO.getPush_id(), configService.loadConfig("APP_LOGO"),  configService.loadConfig("APP_NAME"), "您收到了新的评论", "", JSON.toJSONString(map));*/
		pushservice.pushWindowCommentReplyBack(memberVO.getPush_id());
		return PackageUtil.packageParams(200, "successs", "" , "");
	}
	
	/**
	 * 删除评论
	 * comment_id 评论ID
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteComment" , method = RequestMethod.POST)
	public Object deleteComment(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowCommentVO commentVO) throws Exception {
		if(commentVO.getComment_id() == null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		ShowwindowCommentVO vo = showwindowService.getCommentById(commentVO.getComment_id());
		if(vo == null){
			return PackageUtil.packageParams(15003,"无此评论","",""); 
		}
		if("Y".equals(vo.getDelete_yn())){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(!member_id.equals(vo.getWindow_member()) && !member_id.equals(vo.getCreate_by())){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if("1".equals(vo.getLevel())){
			showwindowService.deleteCommentTop(commentVO.getComment_id());
		}else if("2".equals(vo.getLevel())){
			showwindowService.deleteCommentReply(commentVO.getComment_id());
		}
		return PackageUtil.packageParams(200, "successs", "" , "");
	}
	
	/**
	 * 赞过我的
	 */
	@ResponseBody
	@RequestMapping(value = "/praiseList" , method = RequestMethod.GET)
	public Object praiseList(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowPraiseVO praiseVO) throws Exception {
		praiseVO.setMember_id(member_id);
		int total = showwindowService.getPraiseTotalByMember(praiseVO);
		praiseVO.setPagination(praiseVO.getPage_index(), total+"");
		List<ShowwindowPraiseVO> praiseList = showwindowService.getPraiseListByMember(praiseVO);
		//未读数量
		praiseVO.setRead_yn("N");
		int unread_num = showwindowService.getPraiseTotalByMember(praiseVO);
		//修改为已读
//		showwindowService.updateUnreadPraise(praiseVO);
		
//		MemberMsgVO memberMsg = messageService.getMemberMsg(member_id);
//		int unread_num = Integer.parseInt(memberMsg.getPraise_num());
//		messageService.resetMemberMsg(member_id, "5");
		Map map = new HashMap();
		map.put("praiseList", praiseList);
		map.put("unread_num", unread_num);
		map.put("pagination", praiseVO);
		return PackageUtil.packageParams(200, "successs", map , "");
	}
	
	/**
	 * 评论我的
	 */
	@ResponseBody
	@RequestMapping(value = "/replyList" , method = RequestMethod.GET)
	public Object replyList(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowCommentVO commentVO) throws Exception {
		commentVO.setCreate_by(member_id);
		int total = showwindowService.getReplyTotalByMember(commentVO);
		commentVO.setPagination(commentVO.getPage_index(), total+"");
		List<ShowwindowCommentVO> commentList = showwindowService.getReplyListByMember(commentVO);
		//未读数量
		commentVO.setRead_yn("N");
		int unread_num = showwindowService.getReplyTotalByMember(commentVO);
		//修改为已读
//		showwindowService.updateUnreadReply(commentVO);  需求为单个更变
		Map map = new HashMap();
		map.put("commentList", commentList);
		map.put("unread_num", unread_num);
		map.put("pagination", commentVO);
		return PackageUtil.packageParams(200, "successs", map , "");
	}
	
	/**
	 * 赞已读
	 * praise_id
	 */
	@ResponseBody
	@RequestMapping(value = "/praiseRead" , method = RequestMethod.POST)
	public Object praiseRead(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, String praise_id) throws Exception {
		//修改为已读
		if(praise_id == null || "".equals(praise_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		showwindowService.updatePraiseRead(praise_id);
		return PackageUtil.packageParams(200, "successs", "" , "");
	}
	
	/**
	 * 赞全部已读
	 */
	@ResponseBody
	@RequestMapping(value = "/praiseReadAll" , method = RequestMethod.POST)
	public Object praiseReadAll(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, ShowwindowPraiseVO praiseVO) throws Exception {
		//修改为已读
		praiseVO.setMember_id(member_id);
		showwindowService.updateUnreadPraise(praiseVO);
		return PackageUtil.packageParams(200, "successs", "" , "");
	}
	
	/**
	 * 评论已读
	 * comment_id
	 */
	@ResponseBody
	@RequestMapping(value = "/commentRead" , method = RequestMethod.POST)
	public Object commentRead(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, String comment_id) throws Exception {
		//修改为已读
		if(comment_id == null || "".equals(comment_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		showwindowService.updateCommentRead(comment_id);
		return PackageUtil.packageParams(200, "successs", "" , "");
	}
	
	/**
	 * 评论全部已读
	 * comment_id
	 */
	@ResponseBody
	@RequestMapping(value = "/commentReadAll" , method = RequestMethod.POST)
	public Object commentReadAll(@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,
			HttpServletRequest request, HttpServletResponse response, 
			@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, String comment_id) throws Exception {
		//修改为已读
		showwindowService.updateCommentReadAll(member_id);
		return PackageUtil.packageParams(200, "successs", "" , "");
	}
}
